import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class LoginJFrame extends JFrame implements MouseListener {
    //定义变量
    JLabel accoutJLabel;
    JLabel passwordJLabel;
    JTextField accoutFiled;
    JTextField passwordFiled;
    JLabel loginJLabel;
    JLabel registerJLabel;
    JLabel rl;
    File file = new File("accout.txt");


    public LoginJFrame() {
        //设置位置
        accoutJLabel = new JLabel("账号：");//账号标签
        accoutJLabel.setFont(new Font("华文中宋", 1, 20));
        accoutJLabel.setBounds(80, 45, 100, 20);
        passwordJLabel = new JLabel("密码：");//密码标签
        passwordJLabel.setFont(new Font("华文中宋", 1, 20));
        passwordJLabel.setBounds(80, 100, 100, 20);
        accoutFiled = new JTextField();//账号文本
        accoutFiled.setBounds(145, 40, 300, 30);
        accoutFiled.setFont(new Font("华文中宋", 1, 20));
        passwordFiled = new JTextField();//密码文本
        passwordFiled.setBounds(145, 95, 300, 30);
        passwordFiled.setFont(new Font("华文中宋", 1, 20));
        loginJLabel = new JLabel(new ImageIcon("picture/登录按钮.png"));//登录注册按钮
        loginJLabel.setBounds(70, 175, 128, 47);
        registerJLabel = new JLabel(new ImageIcon("picture/注册按钮.png"));
        registerJLabel.setBounds(300, 175, 128, 47);
        rl = new JLabel(new ImageIcon("picture//登录注册.png"));
        rl.setBounds(0, 0, 500, 300);
//添加到界面中
        this.add(passwordJLabel);
        this.add(accoutJLabel);
        this.add(accoutFiled);
        this.add(passwordFiled);
        this.add(loginJLabel);
        this.add(registerJLabel);
        this.add(rl);
        this.setTitle(" 抽奖系统登录界面");
        this.setSize(500, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        this.setVisible(true);
        //增加点击事件
        loginJLabel.addMouseListener(this);
        registerJLabel.addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        //登录按钮点击事件
        if (e.getSource() == loginJLabel) {
            //获取文本内容
            String accout = accoutFiled.getText();
            String password = passwordFiled.getText();

            try {
                //创建字符流
                BufferedReader br = new BufferedReader(new FileReader(file));
                String str;
                while ((str = br.readLine()) != null) {
                    String[] s = str.split(",");//用逗号分割
                    //判断账号密码是否输入正确
                    if (s[0].equals(accout) && s[1].equals(password)) {
                        //关闭此界面
                        this.setVisible(false);
                        //打开管理员界面或者用户界面
                        new LotteryJFrame();


                        break;
                    }
                }
                //判断是否找到，没找到则弹窗
                if (str == null) {
                    JOptionPane.showConfirmDialog(null, "账号或者密码错误", "提示", JOptionPane.UNDEFINED_CONDITION);
                }

            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }

        }
        //点击登录按钮打开登录界面
        else if (e.getSource() == registerJLabel) {
            this.setVisible(false);
            new RegisterJFrame();


        }


    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}


